#include "../Source/server.hpp"
#include "../Source/http/http.hpp"
class Test
{
private:
    Socket sockfd;
public:
    Test()
    {
        sockfd.ClinetConnect(8088, "111.231.79.105");
    }

    //超时连接测试：客户端只发送一次数据，等待服务器端连接超时关闭连接
    void TimeOutTest()
    {
        string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
        Socket sockfd;
        sockfd.Send((void*)req.c_str(), req.size());
        char buffer[2048] = {0};
        sockfd.Recv(buffer,2047);
        while(1);
    }

    //错误请求测试
    //case 1：仅发送一条错误请求，期望由于读取数据不足而超时关闭连接
    void ErrorRequestCase1()
    {
        string req  = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 100\r\n\r\n";
        sockfd.Send((void*)req.c_str(), req.size());
        char buffer[2048] = {0};
        sockfd.Recv(buffer,2047);
        while(1);
    }

    //case 2：发送多次小请求，期望可能能正确处理一次，但后面会处理出错（因为Content-Length的设置）    
    void ErrorRequestCase2()
    {
        string req  = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 100\r\n\r\n";
        while(1)
        {   
            sockfd.Send((void*)req.c_str(), req.size());
            sockfd.Send((void*)req.c_str(), req.size());
            sockfd.Send((void*)req.c_str(), req.size());
            char buffer[2048] = {0};
            sockfd.Recv(buffer,2047);
            sleep(3);
        }
    }

    void OnHandleTimeOutTest()
    {
        for(int i = 0; i < 10; i++)
        {
            int ret = fork();
            assert(ret != -1);
            if(ret == 0)
            {
                Socket sock;
                sock.ClinetConnect(8088, "111.231.79.105");
                string req  = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
                while(1)
                {   
                    sockfd.Send((void*)req.c_str(), req.size());
                    char buffer[2048] = {0};
                    sockfd.Recv(buffer,2047);
                    sleep(1);
                }
                exit(0);
            }
        }

        while(1) sleep(1);     
    }

    void MultipleRequestTest()
    {
        string req  = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
        req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
        req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
        
        assert(sockfd.Send((void*)req.c_str(), req.size()) != -1); 
        char buffer[2048] = {0};
        while(sockfd.Recv(buffer,2047) > 0)
        {
            cout << buffer << endl;
            memset(buffer, 0, sizeof(buffer));
        }

        return;
    }

    void PutBigFileTest()
    {
        string req_info  = "PUT /123.txt HTTP/1.1\r\nConnection: keep-alive\r\n";
        //读文件，构建请求并发送
        string buffer;
        Util::ReadFile("PutTest.txt", &buffer);
        //Util::ReadFile("little.txt", &buffer);
        req_info += "Content-Length: " + std::to_string(buffer.size()) + "\r\n\r\n";
        req_info += buffer;
        ERR_LOG("%d", req_info.size());
        assert(sockfd.Send((void*)req_info.c_str(), req_info.size()) != -1);

        char buff[2048] = {0};
        while(sockfd.Recv(buff,2047) > 0)
        {
            cout << buff << endl;
            memset(buff, 0, sizeof(buff));
        }

        return;
    }

};


int main()
{
    signal(SIGCHLD,SIG_IGN);
    Test t;
    //t.OnHandleTimeOutTest();
    //t.MultipleRequestTest();
    t.PutBigFileTest();

    return 0;
}